import { ref } from 'vue'
import { defineStore } from 'pinia'

export interface AppointmentFilterState {
  status: string
  name: string
  startDate: string
  endDate: string
}

export interface CurrentAppointmentState {
  id: number
  userId: number
  patientName: string
  status: string
  createTime: string
  expireTime: string
}

export const useAppointmentStore = defineStore(
  'appointment',
  () => {
    // 筛选条件状态
    const filterStatus = ref('')
    const filterName = ref('')
    const filterStartDate = ref('')
    const filterEndDate = ref('')

    // 当前预约信息
    const currentAppointmentId = ref<number | null>(null)
    const currentUserId = ref<number | null>(null)
    const currentPatientName = ref('')
    const currentStatus = ref('')
    const currentCreateTime = ref('')
    const currentExpireTime = ref('')

    // 设置筛选状态
    function setFilterStatus(status: string) {
      filterStatus.value = status
    }

    // 设置筛选医生姓名
    function setFilterName(name: string) {
      filterName.value = name
    }

    // 设置筛选开始日期
    function setFilterStartDate(date: string) {
      filterStartDate.value = date
    }

    // 设置筛选结束日期
    function setFilterEndDate(date: string) {
      filterEndDate.value = date
    }

    // 设置所有筛选条件
    function setAllFilters(filters: Partial<AppointmentFilterState>) {
      if (filters.status !== undefined) filterStatus.value = filters.status
      if (filters.name !== undefined) filterName.value = filters.name
      if (filters.startDate !== undefined) filterStartDate.value = filters.startDate
      if (filters.endDate !== undefined) filterEndDate.value = filters.endDate
    }

    // 清除所有筛选条件
    function clearAllFilters() {
      filterStatus.value = ''
      filterName.value = ''
      filterStartDate.value = ''
      filterEndDate.value = ''
    }

    // 设置当前预约信息
    function setCurrentAppointment(appointment: CurrentAppointmentState) {
      currentAppointmentId.value = appointment.id
      currentUserId.value = appointment.userId
      currentPatientName.value = appointment.patientName
      currentStatus.value = appointment.status
      currentCreateTime.value = appointment.createTime
      currentExpireTime.value = appointment.expireTime
    }

    // 清除当前预约信息
    function clearCurrentAppointment() {
      currentAppointmentId.value = null
      currentUserId.value = null
      currentPatientName.value = ''
      currentStatus.value = ''
      currentCreateTime.value = ''
      currentExpireTime.value = ''
    }

    return {
      filterStatus,
      filterName,
      filterStartDate,
      filterEndDate,
      setFilterStatus,
      setFilterName,
      setFilterStartDate,
      setFilterEndDate,
      setAllFilters,
      clearAllFilters,
      // 当前预约相关
      currentAppointmentId,
      currentUserId,
      currentPatientName,
      currentStatus,
      currentCreateTime,
      currentExpireTime,
      setCurrentAppointment,
      clearCurrentAppointment
    }
  },
  {
    persist: true // 开启持久化
  }
)
